package com.dream.testtubebaby_doctor.utils;

import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

public class SharePreferencesManager {
	private static final String TAG = "SharePreferencesManager";
	private SharedPreferences userInfo;
	private Context mContext;
	public SharePreferencesManager(Context context) {
		userInfo = context.getSharedPreferences("user_info", 0);
		mContext = context;
	}

	public class LoginInfo {
		private String mName;
		private String mPassword;

		public LoginInfo() {
		}

		public LoginInfo(String name, String password) {
			this.mName = name;
			this.mPassword = password;
		}
		public String getName(){
			return mName;
		}
		public String getPassword(){
			return mPassword;
		}
	}

	public LoginInfo readUserInfo() {
		LoginInfo user = new LoginInfo();
		user.mName = userInfo.getString("user_name", "");
		user.mPassword = userInfo.getString("user_password", "");
		return user;
	}

	public void writeUserInfo(String name, String password) {
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putString("user_name", name);
		editor.putString("user_password", password);
		editor.commit();
	}

	public void writeUserAccessToken(String accessToken){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putString("user_accessToken", accessToken);
		editor.commit();
	}
	
	public String readUserAccessToken(){
		return userInfo.getString("user_accessToken", null);
	}
	
	public void writeUserId(long id){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putLong("user_id", id);
		editor.commit();
	}
	
	public long readUserId(){
		try {
			return userInfo.getLong("user_id", 0L);
		} catch (Exception e) {
			e.printStackTrace();
			return 0L;
		}
	}

	public void writeUserName(String name){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putString("user_name", name);
		editor.commit();
	}
	
	public String readUserName(){
		return userInfo.getString("user_name", null);
	}
	
	public void writePhotoUrl(String name){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putString("user_photourl", name);
		editor.commit();
	}
	
	public String readPhotoUrl(){
		return userInfo.getString("user_photourl", null);
	}
	
	public void writeGuideFlag(boolean guide_flag){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putBoolean("guide_flag", guide_flag);
		editor.commit();
	}
	
	public boolean readGuideFlag(){
		return userInfo.getBoolean("guide_flag", true);
	}
	
	public void writeNofityFlag(int nofity_flag){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putInt("nofity_flag", nofity_flag);
		editor.commit();
	}
	
	public int readNofityFlag(){
		return userInfo.getInt("nofity_flag", 0);
	}
	
	public void switchLanguage(String language) {
		Resources resources = mContext.getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
       if (language.equals("en")) {
            config.locale = Locale.ENGLISH;
        } else {
        	 config.locale = Locale.SIMPLIFIED_CHINESE;
        }
        resources.updateConfiguration(config, dm);
        
        writeLanguageCode(language);
    }
	
	public void writeLanguageCode(String languageCode){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putString("language_code", languageCode);
		editor.commit();
	}
	
	public String readLanguageCode(){
		return userInfo.getString("language_code", "zh");
	}
	
	public void cleanOffUserInfo(){
		SharedPreferences.Editor editor = userInfo.edit();
//		editor.clear();
		editor.putLong("user_id", 0);
		editor.commit();
	}
}
